<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<title></title>
		<script src="js/mui.min.js"></script>
		<script src="js/jquery-1.11.0.js"></script>
		<script src="js/common.js"></script>
		<script src="js/loaddata.js"></script>
		<link href="css/mui.min.css" rel="stylesheet" />
		<link href="css/maytao.css" rel="stylesheet" />
	</head>

	<body>
		<header class="mui-bar mui-bar-nav">
			<div class="mui-input-row mui-search">
				<input type="search" id="btnsearch" onkeyup="enterSearch(event)" class="mui-input-clear" placeholder="搜索">

			</div>

		</header>
		<div class="mui-content">
			<div id="container" class="mui-content mui-scroll-wrapper">
				<div class="mui-scroll" id="content">
				</div>
			</div>
			<a class="top" id="top"><span class="mui-icon mui-icon-arrowup"></span><br/>顶部</a>
		</div>

	</body>
	<script type="application/javascript" charset="UTF-8">
		var cate = "";
		$(document).ready(function() {
			cate = localStorage.cateId;
			if(cate == "null"||cate==""||cate==null) 
			{} 
			else {
				sendRequest('0', '', cate);
				localStorage.cateId = null;
			}
			ToTop('top');
		})

		mui.init({

			pullRefresh: {
				container: '#container',

				up: {
					contentrefresh: '正在加载...',
					callback: pullupRefresh
				}
			}
		});

		//点击键盘搜索时间
		function enterSearch(e) {
			var keyWord = $("#btnsearch").val();
			if(e.keyCode == 13) {
				if(keyWord == "") {
					mui.toast("搜索内容不能为空");
					return;
				}

				document.activeElement.blur();
				//首次查询id=0
				$("#content").html("");
				SearchData("0", keyWord)

			}
		}
		//查询
		function SearchData(id, keyword) {
			mui.plusReady(function() {
				plus.nativeUI.showWaiting();
			});
			if((id == "" || keyword.trim() == "") && (cate == null || cate == "")) {

				return;
			}
			sendRequest(id, keyword, cate);

		}
		//发送请求
		function sendRequest(id, keyword, cate) {
			if(keyword != null && keyword != "") {
				cate = "";
			}

			$.post("http://www.maytao.cn/Home/QueryDataByKeyName", {
				id: id,
				searchKey: keyword,
				cateId: cate
			}, function(data) {
				mui.plusReady(function() {
					plus.nativeUI.closeWaiting();
				});
				if(data.Code == 0) {

					var jsonobj = eval(data.Data)
					//记录最后的id
					var lstid = jsonobj[jsonobj.length - 1].ProductId + "";
					plus.storage.setItem("lstid", lstid)

					var content = AppendHtml(jsonobj);
					$("#content").append(content);
				} else {
					mui.toast("别扯了，到底了");
				}

			});
		}

		/**
		 * 上拉加载具体业务实现
		 */
		function pullupRefresh() {
			setTimeout(function() {
				mui('#container').pullRefresh().endPullupToRefresh(); //参数为true代表没有更多数据了。
				var keyword = $("#btnsearch").val();
				var id = plus.storage.getItem("lstid")
				if(id == null || id == "") {
					return
				}

				SearchData(id, keyword)
			}, 1500);
		}
	</script>

</html>